package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/*
 * 切面类：提供增强方法的类
 * */
@Component
//声明当前bean对象为切面
@Aspect
public class LogAop {

    @Pointcut("execution(* com.itheima.dao.*.*(..))")
    public void pid(){}

    //@Before("pid()")
    public void before(){
        System.out.println("前置增强");
    }

    //@AfterReturning("pid()")
    public void afterRunning(){
        System.out.println("后置增强");
    }

    //@AfterThrowing("pid()")
    public void throwRunning(){
        System.out.println("异常增强");
    }

    //@After("pid()")
    public void after(){
        System.out.println("最终增强");
    }


    @Around("pid()")
    public Object around(ProceedingJoinPoint point){

        System.out.println("环绕通知"+point.getTarget());
        System.out.println("方法名称："+point.getSignature().getName());
        Object obj=null;
        try {
            //前置通知
            before();
            obj=point.proceed();//继续执行目标方法
            //后置通知
            afterRunning();
        } catch (Throwable e) {
            //异常通知
            throwRunning();
            e.printStackTrace();
        }finally {
            //最终通知
            after();
        }
        return obj;
    }


}
